package com.shroo8.footballbloglib.dao.footballblogwrite;

import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.stereotype.Repository;

import com.shroo8.footballbloglib.dao.constants.DataAccessConstants;
import com.shroo8.footballbloglib.dao.util.DaoUtil;

@Repository(value="createArticleDao")
public class CreateArticleDao {
	@Resource(name="dataSourceFootballblogWrite")
	protected DataSource dataSource;
	
	protected final String sql = DataAccessConstants.SP_CREATE_ARTICLE;
	
	@Resource(name="daoUtil")
	protected DaoUtil daoUtil;

	public String execute(String authorId, String articleTitle, String articleText){
		List<SqlParameter> sqlParameters = new ArrayList<SqlParameter>();
	    Map<String, Object> inParams = new HashMap<String, Object>();
/*
 * 	Parameter list:
	   	in_author_id VARCHAR(36),
		in_article_title VARCHAR (100),
		in_article_text VARCHAR (10000),
		OUT out_article_id VARCHAR(36)
*/
	    sqlParameters.add(new SqlParameter(DataAccessConstants.ARTICLES_AUTHOR_ID, Types.VARCHAR));
        inParams.put(DataAccessConstants.ARTICLES_AUTHOR_ID, authorId);

	    sqlParameters.add(new SqlParameter(DataAccessConstants.ARTICLES_ARTICLE_TITLE, Types.VARCHAR));
        inParams.put(DataAccessConstants.ARTICLES_ARTICLE_TITLE, articleTitle);

	    sqlParameters.add(new SqlParameter(DataAccessConstants.ARTICLES_ARTICLE_TEXT, Types.VARCHAR));
        inParams.put(DataAccessConstants.ARTICLES_ARTICLE_TEXT, articleText);

        sqlParameters.add(new SqlOutParameter(DataAccessConstants.SP_CREATE_ARTICLE_OUT_PARAMETER, Types.VARCHAR));
	    
        Map<String, Object> out = daoUtil.spExecute(dataSource, sql,
                sqlParameters, inParams);

		return (String) out.get(DataAccessConstants.SP_CREATE_ARTICLE_OUT_PARAMETER);
	}
	
	public String getSql() {
		return sql;
	}
	public DataSource getDataSource() {
		return dataSource;
	}
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public DaoUtil getDaoUtil() {
		return daoUtil;
	}

	public void setDaoUtil(DaoUtil daoUtil) {
		this.daoUtil = daoUtil;
	}

	
}
